package com.labweb.filtro;

import java.io.IOException;

import javax.faces.context.FacesContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.labweb.beans.UsuarioLogado;

public class FiltroLogin implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub

		HttpServletRequest req = (HttpServletRequest) request;
		UsuarioLogado usuarioLogado = (UsuarioLogado) req.getSession().getAttribute("usuarioLogado"); 
  
        if (usuarioLogado != null) {  
            chain.doFilter(request, response);  
        } else {  
            HttpServletResponse res = (HttpServletResponse) response;  
            res.sendRedirect(req.getContextPath() + "/faces/login.jsp");  
        }  
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}



}
